package mvc;


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Iterator;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

import mvc.ControladorGps.Pantalla;


@SuppressWarnings("serial")
public class VistaMenu extends JPanel {

	
	public JFrame frame;
	
	private ControladorGps controlador;
	
	JPanel panelNewORegist;

	JPanel panelNewUser;
	JTextField jtName;

	JPanel panelRegist;
	JComboBox cmbUser;
	
	JPanel panelVehiculo;
	JComboBox cmbVehiculo;
	
	JPanel panelNivel;
	JPanel panelPlayAgain;
	JPanel panelGuardar;
	
	
	
	public VistaMenu (final VistaMapa vistaJuego,final ControladorGps controlador){
	
		//agrego las referencias
		this.controlador = controlador;
		
		
		frame = new JFrame ("GPS");
		frame.add(vistaJuego);		
		frame.setBounds(0, 0, 1000, 680);
		//cerrar ventana.
		frame.addWindowListener(new CloseListener());

		frame.setVisible(true);
		frame.getContentPane().setLayout(null);


		armarPanelNewRegistrado();
		armarPanelNewUser();
		armarPanelRegist();
		armarPanelVehiculo();
		armarPanelNivel();
	
		armarPanelPlayAgain();
		armarPanelGuardar();
		

		panelNewORegist.setBounds(350, 50, 254, 500);
		frame.getContentPane().add(panelNewORegist);
	
		panelNewUser.setBounds(350, 50, 334, 180);
		frame.getContentPane().add(panelNewUser);
		
		panelRegist.setBounds(350, 50, 334, 240);
		frame.getContentPane().add(panelRegist);
		
		panelVehiculo.setBounds(350, 50, 334, 240);
		frame.getContentPane().add(panelVehiculo);
		
		panelNivel.setBounds(350, 50, 358, 49);
		frame.getContentPane().add(panelNivel);
		
		
		panelPlayAgain.setBounds(350, 650, 200, 49);
		frame.getContentPane().add(panelPlayAgain);
		
		panelGuardar.setBounds(375, 650, 100, 49);
		frame.getContentPane().add(panelGuardar);
		
		frame.setVisible(true);
		
	}
	
	public void armarPanelNewRegistrado(){		
		JButton btNuevo = new JButton("Nuevo Usuario");
		btNuevo.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				panelNewORegist.setVisible(false);
				panelNewUser.setVisible(true);
				frame.repaint();
			}
			});

		JButton btUserRegist = new JButton("Usuario Registrado");
		btUserRegist.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				panelNewORegist.setVisible(false);
				panelRegist.setVisible(true);
				frame.repaint();
			}
			});
	    
		panelNewORegist = new JPanel();
		panelNewORegist.add(btNuevo);
		panelNewORegist.add(btUserRegist);
		panelNewORegist.setVisible(true);
	}

	public void armarPanelNewUser(){
		
		JButton btNewUser = new JButton("Aceptar");
		btNewUser.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			if(jtName.getText().length() > 0){
				panelNewUser.setVisible(false);
				panelVehiculo.setVisible(true);
				controlador.nuevoJugador(jtName.getText());
				frame.repaint();
			}
		}
		});
		
		
		JButton btAtrasNewUsr = new JButton("Atras");
		btAtrasNewUsr.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			panelNewUser.setVisible(false);
			panelNewORegist.setVisible(true);
			frame.repaint();
		}
		});
		
		jtName = new JTextField(10);
		
		panelNewUser = new JPanel();
		panelNewUser.add(jtName);
		panelNewUser.add(btNewUser);
		panelNewUser.add(btAtrasNewUsr);
		panelNewUser.setVisible(false);
		
	}
	
	public void armarPanelRegist(){
		JButton btnNueva = new JButton("Nueva Partida");
		btnNueva.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(((String) cmbUser.getSelectedItem()).length() > 0){
					panelRegist.setVisible(false);
					panelVehiculo.setVisible(true);
					controlador.nuevoJugador((String) cmbUser.getSelectedItem());
					frame.repaint();
				}
			}
		});
		
		cmbUser = new JComboBox();
		Iterator<String> iter = controlador.iteradorListaJugadores();
		while (iter.hasNext()){
			cmbUser.addItem(iter.next());
		}
		
		JButton btAtrasRe = new JButton("Atras");
		btAtrasRe.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				panelRegist.setVisible(false);
				panelNewORegist.setVisible(true);
				frame.repaint();
			}
		});
		
		JButton btnCargar = new JButton("Cargar");
		btnCargar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(((String) cmbUser.getSelectedItem()).length() > 0){
					panelRegist.setVisible(false);
					controlador.cargarJugador((String) cmbUser.getSelectedItem());
				}
			}
		});
		
		panelRegist = new JPanel();
		panelRegist.add(cmbUser);
		panelRegist.add(btnNueva);
		panelRegist.add(btAtrasRe);
		panelRegist.add(btnCargar);
		
		panelRegist.setVisible(false);
		
		
	}
	public void armarPanelNivel(){
		JButton btNiv1 = new JButton("NIVEL 1");
		btNiv1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				controlador.nuevoJuego(1);
				panelNivel.setVisible(false);
				frame.repaint();
			}
		});

		JButton btNiv2 = new JButton("NIVEL 2");
		btNiv2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				controlador.nuevoJuego(2);
				panelNivel.setVisible(false);
				frame.repaint();
			}
			});
		
		JButton btNiv3 = new JButton("NIVEL 3");
		btNiv3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				controlador.nuevoJuego(3);
				panelNivel.setVisible(false);
				frame.repaint();
			}
			});


			panelNivel = new JPanel();
			panelNivel.add(btNiv1);
			panelNivel.add(btNiv2);
			panelNivel.add(btNiv3);
			panelNivel.setVisible(false);
	}
	
	public void armarPanelVehiculo(){
		JButton btnVehiculo = new JButton("Aceptar");
		btnVehiculo.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				panelVehiculo.setVisible(false);
				panelNivel.setVisible(true);
				controlador.seleccionVehiculo((String) cmbVehiculo.getSelectedItem());
				frame.repaint();
			}
		});
		
		cmbVehiculo = new JComboBox();
		
		Iterator<String> iter = controlador.iteradorVehiculos();
		while (iter.hasNext()){
			cmbVehiculo.addItem(iter.next());
		}
		
		panelVehiculo = new JPanel();
		panelVehiculo.add(cmbVehiculo);
		panelVehiculo.add(btnVehiculo);
		
		panelVehiculo.setVisible(false);
		
	}
	
	public void armarPanelGuardar(){		
		JButton btGuardar = new JButton("Guardar");
		btGuardar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				controlador.guardar();
				controlador.pantalla = Pantalla.MAPA;
				panelGuardar.setVisible(false);
			}
		});

		panelGuardar = new JPanel();
		panelGuardar.add(btGuardar);	
		panelGuardar.setVisible(false);

	}

	public void armarPanelPlayAgain(){		
		JButton btPlayAgain = new JButton("Nuevo Juego");
		btPlayAgain.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				controlador.nuevoJuego();
				panelPlayAgain.setVisible(false);
			}
			});

		panelPlayAgain = new JPanel();
		panelPlayAgain.add(btPlayAgain);
		panelPlayAgain.setVisible(false);

	}

	
	public static class CloseListener extends WindowAdapter
	{	public void windowClosing(WindowEvent e)
		{	e.getWindow().setVisible(false);
			System.exit(0);
		}
	}
	
	
}
